import 'package:lib_util/lib_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:lib_database/objectbox.dart';
import 'package:lib_database/src/model/user_settings_model/user_settings_model.dart';

class UserSettingsProvider extends ChangeNotifier {
  int tabIndex = 0;
  late String uId;

  UserSettingsProvider() {
  }

  initData(String userId) {
    uId = userId;
  }

  UserSettings get settingsFromDB => ObjectBox().userSettingsModel.getUserSettingsByUId(uId);

  changeTab(int index) {
    if (index != tabIndex) {
      tabIndex = index;
      notifyListeners();
    }
  }

  updateSettings({String? password, bool? biometric}) {
    UserSettings settings = ObjectBox().userSettingsModel.getUserSettingsByUId(uId);
    if (null != password) {
      settings.password = password;
    }
    if (null != biometric) {
      settings.biometrics = biometric;
    }
    ObjectBox().userSettingsModel.updateUserSettings(settings);

    notifyListeners();
  }

  updateBiometric(bool biometric) {
    UserSettings settings = ObjectBox().userSettingsModel.getUserSettingsByUId(uId);
    settings.biometrics = biometric;
    ObjectBox().userSettingsModel.updateUserSettings(settings);

    notifyListeners();
  }

  bool isBiometricPay() {
    return settingsFromDB.biometrics;
  }

  bool hasPassword() {
    return Utils.isNotEmpty(settingsFromDB.password);
  }
}
